package otherSAMS.dao;

import dao.Dao;
import otherSAMS.jdbc.DBconnection;
import otherSAMS.model.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDao implements Dao<Student> {
    public static List<Student> selectByStudentName(String studentName) {
        //1.创建连接
        Connection connection = DBconnection.getConnection();
        //2.预编译Sql
        String sql = "\tSELECT\n" +
                "\t*\n" +
                "FROM\n" +
                "\tstudent \n" +
                "WHERE\n" +
                "\tstudent.NAME =?";
        //PreparedStatement prepareStatement = null;
        List<Student> list = new ArrayList<>();
        ResultSet resultSet = null;
        PreparedStatement prepareStatement = null;
        try {
            prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setString(1,"%"+studentName+"%");
            resultSet = prepareStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                int sex = resultSet.getInt(3);
                String telephone = resultSet.getString(4);
                int studentId = resultSet.getInt(5);
                int clazzId = resultSet.getInt(6);
                int userId = resultSet.getInt(7);
                Student student = new Student(id, name, sex, telephone,studentId, clazzId, userId);
                list.add(student);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.关闭连接
            DBconnection.closeConnection(resultSet,prepareStatement,connection);
        }
        return list;
    }

    @Override
    public boolean insert(Student student) {
        return false;
    }

    @Override
    public boolean delete(int id) {
        return false;
    }

    @Override
    public boolean update(Student student) {
        return false;
    }

    @Override
    public Student selectByld(int id) {
        return null;
    }

    @Override
    public Student selectAll() {
        return null;
    }
}
